package com.example.demo.json;



import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.junit.Test;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;

import java.util.Arrays;
import java.util.HashMap;

public class MapJson {
    public static void main(String[] args) throws JSONException {

        StringBuffer sb = new StringBuffer().append("\"");
        change(sb);
        System.out.println(sb.toString());//"235466

    }
    private static void change(StringBuffer sb){
        sb.append("235466");
    }

    @Test
    public void StringJson() throws JSONException {
        String json = "{\"name\":\"zhangsan\",\"password\":\"123123\"}";
        String str = "name,xiaomin";
        JSONObject jsonArray = new JSONObject(json);
        System.out.println(jsonArray);
        //{"password":"123123","name":"zhangsan"}

       /* str = "name,xiaomin";
        jsonArray = new JSONArray(str);
        System.out.println(jsonArray);*/
        //SMS_272520204

    }

    @Test
    public void MapJson(){
        HashMap<String, String> map = new HashMap<>();
        map.put("code1","1234");
        map.put("code2","5678");
        JSONObject fromObject01 = new JSONObject(map);
        StringBuffer params = new StringBuffer();
        params.append("[");
        params.append(fromObject01).append(",");
        map.put("code1","abcd");
        map.put("code2","efgh");
        JSONObject fromObject02 = new JSONObject(map);
        params.append(fromObject01).append("]");
        System.out.println(params.toString());

        //[{"code2":"5678","code1":"1234"},{"code2":"5678","code1":"1234"}]
    }

    @Test
    public void ListJson(){
        Object o = com.alibaba.fastjson.JSONObject.toJSON("wewe,wewe");
        System.out.println(o.toString());
        Object o1 = JSON.toJSON("wewe,wewe");
        Object o2 = JSON.parseObject("wewe,wewe");
        System.out.println(o1.toString());
        System.out.println(o2.toString());
        //JSON.parseArray用于如果是用[] 包裹的
        //JSON.parseObject用于如果是用{}包裹的
        String s="[{recordsTotal:0,draw:0,recordsFiltered:1}]";
        JSONArray jsonArray=JSON.parseArray(s);
        JSONArray jsonArray1= JSONArray.parseArray(s);
        System.out.println(jsonArray.toString());
        System.out.println(jsonArray1.toString());
    }

}


